package com.wonders.common.web.captcha;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

import com.google.code.kaptcha.GimpyEngine;
import com.google.code.kaptcha.NoiseProducer;
import com.google.code.kaptcha.util.Configurable;
import com.jhlabs.image.RippleFilter;
import com.jhlabs.image.ShadowFilter;

/**
 * 验证码样式引擎
 * 
 * @author WuBin
 * 
 */
public class SlightGimpy extends Configurable implements GimpyEngine {

	public BufferedImage getDistortedImage(BufferedImage baseImage) {
		NoiseProducer noiseProducer = getConfig().getNoiseImpl();
		BufferedImage distortedImage = new BufferedImage(baseImage.getWidth(),
				baseImage.getHeight(), 2);

		Graphics2D graph = (Graphics2D) distortedImage.getGraphics();

		ShadowFilter shadowFilter = new ShadowFilter();
		shadowFilter.setRadius(10.0F);
		shadowFilter.setDistance(5.0F);
		shadowFilter.setOpacity(1.0F);

		Random rand = new Random();

		RippleFilter rippleFilter = new RippleFilter();
		rippleFilter.setWaveType(0);
		rippleFilter.setXAmplitude(0F);
		rippleFilter.setYAmplitude(new Float(rand.nextFloat() * 0.5));
		rippleFilter.setXWavelength(rand.nextInt(2) + 2);
		rippleFilter.setYWavelength(rand.nextInt(2) + 2);
		rippleFilter.setEdgeAction(1);

		BufferedImage effectImage = rippleFilter.filter(baseImage, null);
		effectImage = shadowFilter.filter(effectImage, null);

		graph.drawImage(effectImage, 0, 0, null, null);
		graph.dispose();

		noiseProducer.makeNoise(distortedImage, 0.1F, 0.1F, 0.25F, 0.25F);
		noiseProducer.makeNoise(distortedImage, 0.1F, 0.25F, 0.5F, 0.4F);
		return distortedImage;
	}
}